<template>

    <rx-dialog @handOk="handleSubmit" :oktext="i18n('list.connectBtn','连接测试')"   :order="'top'" btnalign="right" :showcancel="false" >
        <div slot="toolbar">
            <a-button @click="handSave" type="danger">{{ $ti18('rxForm.saveBtn','保存') }}</a-button>
        </div>
        <a-form-model ref="form"  :model="form" :rules="rules" :layout="'vertical'">
            <a-form-model-item  :label="i18n('form.host','服务器地址')" prop = "host">
                <a-input :placeholder="i18n('form.host','服务器地址','input')" v-model="form.host"

                />
            </a-form-model-item>

            <a-form-model-item  :label="i18n('form.port','服务器端口')" prop = "port">
                <a-input-number :min="0" :max="1000" :placeholder="i18n('form.port','服务器端口','input')"
                                v-model="form.port"   />
            </a-form-model-item>
            <a-form-model-item  :label="i18n('form.username','帐号')" prop = "username">
                <a-input  :placeholder="i18n('form.username','帐号','input')"
                          v-model="form.username"/>
            </a-form-model-item>
            <a-form-model-item  :label="i18n('form.password','密码')" prop = "password">
                <a-input  :placeholder="i18n('form.password','密码','input')"
                          v-model="form.password" />
            </a-form-model-item>
        </a-form-model>

    </rx-dialog>


</template>

<script>

import EmailApi from "@/api/system/core/email";
import SysPropertiesApi from "@/api/system/core/sysProperties";

export default {
    name: "EmailValid",
    data(){
        return {
            labelCol: {span: 6},
            wrapperCol: {span: 18},

            rules: {
                host: [
                    { required: true, message: this.i18n('msa.inputHost','请输入邮箱服务器地址'), trigger: 'blur' },
                ],
                port: [
                    { required: true, message: this.i18n('msa.inputPort','请输入邮箱服务器端口'), trigger: 'blur' },
                ],
                username: [
                    { required: true, message: this.i18n('msa.inputUsername','请输入邮箱登录帐号'), trigger: 'blur' },
                ],
                password: [
                    { required: true, message: this.i18n('msa.inputPassword','请输入邮箱登录密码'), trigger: 'blur' },
                ],
                receiver: [
                    { required: true, message: this.i18n('msa.addressee','请输入收件人','input'), trigger: 'blur' },
                ],

            },
            form:{
                host:"",
                port:25,
                username:"",
                password:"",
            }
        }
    },
    created(){
        this.getConfig();
    },
    methods:{
        i18n(name,text,key){
            return this.$ti18(name,text,"EmailLang",key);
        },
        getConfig() {
            SysPropertiesApi.getConfigMap().then(res => {
                let data=res.data;
                this.form.host=data["email.host"];
                this.form.port=data["email.port"];
                this.form.username=data["email.username"];
                this.form.password=data["email.password"];
            })
        },
        handleSubmit(vm){
            this.$refs.form.validate(valid => {
                if (valid) {
                    EmailApi.testConnect(this.form).then(res=>{
                        vm.loading=false;
                    });
                } else {
                    vm.loading=false;
                    return false;
                }
            });

        },
        handSave(){
            let self_=this;
            this.$confirm({
                title: this.$ti18('rxMsa.tipsInfo', '提示信息'),
                content: this.i18n('msa.saveMailTip','确认保存邮件配置吗?'),
                okText: this.$ti18('rxForm.ackBtn', '确认'),
                cancelText: this.$ti18('rxForm.cancelBtn', '取消'),
                onOk(){
                    let params={
                        "host":self_.form.host,
                        "port":self_.form.port,
                        "username":self_.form.username,
                        "password":self_.form.password
                    }
                    EmailApi.saveEmailConfig(params)
                }
            });

        }
    }


}
</script>

<style scoped>

</style>